package com.example.pinkcat.activity.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.PagerAdapter;


import com.example.pinkcat.R;
import com.example.pinkcat.activity.api.getData.GetData;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class VP_viewpager_adapter extends PagerAdapter {
    private Context context;
    private String[] titles;
    private ArrayList<View> views;  //每个选项卡对应的布局
    String up_id="-1";
    String v_id;
    String labs;
    VideoView videoView;
    EditText pltxt;


    public VP_viewpager_adapter(String[] titles, ArrayList<View> views, Context context,String up_id,String v_id,String labs,VideoView videoView){
        this.titles=titles;
        this.views=views;
        this.context=context;
        this.up_id=up_id;
        this.v_id=v_id;
        this.labs=labs;
        this.videoView=videoView;
    }

    //实例化选项卡
    @NotNull
    @SuppressLint("ShowToast")
    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View v = views.get(position);
        container.addView(v);
        if (position==1){//评论页面
            //评论列表
            RecyclerView recyclerView=v.findViewById(R.id.video_viewpager_page2_recycleview);
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
            VP2_recyleview_adapter adapter=new VP2_recyleview_adapter(context,20);
            recyclerView.setAdapter(adapter);
            //获取评论数据
            GetData.getVideoComment2(context,v_id,adapter);

            //排序方式
            TextView sort_way_viewbnt=v.findViewById(R.id.text_sort_way);//按钮
            TextView textView=v.findViewById(R.id.text_pl);//排序说明

            pltxt=v.findViewById(R.id.editepltxt);
            v.findViewById(R.id.send_plbtn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (pltxt.getText().toString().length()>0){
                        v.findViewById(R.id.send_plbtn).setEnabled(false);
                        if (GetData.sendvideo_pl(v_id,pltxt.getText().toString(),context)){
                            pltxt.setText(" ");
                            GetData.getVideoComment2(context,v_id,adapter);
                        }
                        v.findViewById(R.id.send_plbtn).setEnabled(true);
                    }else {
                        Toast.makeText(context,"评论字数太少了",Toast.LENGTH_SHORT).show();
                    }
                }
            });
            sort_way_viewbnt.setOnClickListener(new View.OnClickListener() {
                @SuppressLint("ShowToast")
                @Override
                public void onClick(View v) {
                    if (sort_way_viewbnt.getText().equals("≡按点赞")){
                        textView.setText(R.string.new_pl);
                        sort_way_viewbnt.setText(R.string.sort_by_time);
                    }
                    else if(sort_way_viewbnt.getText().equals("≡按时间")) {
                        textView.setText(R.string.like_pl);
                        sort_way_viewbnt.setText(R.string.sort_by_like);
                    }
                }
            });
        }
        else{//视频简介页面
            RecyclerView recyclerViewe=v.findViewById(R.id.video_viewpager_page1_recycleview);
            recyclerViewe.setLayoutManager(new LinearLayoutManager(context));
            VP1_recyleview_adapter adapters=new VP1_recyleview_adapter(context,up_id,videoView,v_id);
            recyclerViewe.setAdapter(adapters);


            //获取播放视频信息
            GetData.getVideoinfo1(context,v_id,adapters);
            //获取相关视频
            //获取根据标签获取数据
            GetData.getSameVideo(labs,v_id,context,adapters);//获取并设置recycleview视频数据
        }
        return v;
    }

    //删除选项卡
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView(views.get(position));
    }
    //返回选项大小
    @Override
    public int getCount() {
        return views==null?0:views.size();
    }

    //判断是否是为返回选项卡
    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view==object;
    }

    //获取标题
    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}
